import 'package:adb_tool/base/base_page_state.dart';
import 'package:adb_tool/common/app_color.dart';
import 'package:adb_tool/page/file_manager/file_mannager_page.dart';
import 'package:adb_tool/page/home/home_page.dart';
import 'package:adb_tool/page/logcat/logcat_page.dart';
import 'package:adb_tool/page/main/main_view_model.dart';
import 'package:adb_tool/page/media_management/media_management_page.dart';
import 'package:adb_tool/widgets/sda.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../../widgets/pop_up_menu_button.dart';
import '../../widgets/text_view.dart';

class MainPage extends StatefulWidget {
  const MainPage({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _MainPageState();
  }
}

class _MainPageState extends BasePageState<MainPage, MainViewModel> with SingleTickerProviderStateMixin {
  late PageController tabController;

  @override
  initState() {
    tabController = PageController();
    super.initState();
    viewModel.init();
  }

  @override
  void dispose() {
    super.dispose();
    tabController.dispose();
  }

  @override
  buildContentView(BuildContext context) {
    return CustomPaint(
        foregroundPainter: TagPainter(
            layoutDirection: TextDirection.ltr,
            message: "@zavier",
            location: BannerLocation.topEnd,
            textDirection: TextDirection.ltr),
        child: Row(
          children: [
            Container(
              width: 78,
              color: AppColors.leftMenuColor,
              child: Column(
                children: [
                  Expanded(
                    child: Column(
                      children: _buildMainTabItem(),
                    ),
                  ),
                  InkWell(
                    child: const Padding(
                      padding: EdgeInsets.all(10),
                      child: Text("清理", style: TextStyle(fontSize: 14, color: AppColors.e6)),
                    ),
                    onTap: () {
                      viewModel.clearImageCache();
                    },
                  ),
                ],
              ),
            ),
            Expanded(
                child: Column(
              children: [
                Text(
                  "注意:软件仅限于个人测试,请勿用于商业活动 -@zavier-v${viewModel.buildNumber}",
                  style: const TextStyle(color: Colors.redAccent, fontSize: 13),
                ),
                SizedBox(
                  height: 64,
                  child: Row(
                    children: [
                      Flexible(
                        child: menuView(
                          "设备",
                          "未连接设备",
                          viewModel.devicesViewModel,
                          () {
                            viewModel.getDeviceList();
                          },
                        ),
                        flex: 2,
                      ),
                      Flexible(
                        child: packageNameView(context),
                        flex: 3,
                      ),
                      Flexible(
                        child: InkWell(
                          child: Container(
                            padding: const EdgeInsets.all(8),
                            child: Row(
                              mainAxisSize: MainAxisSize.min,
                              children: const [
                                Text("刷新",style: TextStyle(color: AppColors.c33),),
                                Icon(
                                  Icons.refresh,
                                  color: Colors.black38,
                                )
                              ],
                            ),
                          ),
                          onTap: () {
                            viewModel.refresh();
                          },
                        ),
                        flex: 1,
                      )
                    ],
                  ),
                ),
                Expanded(child: Consumer<MainViewModel>(
                  builder: (context, value, child) {
                    var isEmpty = value.deviceId.isEmpty || value.packageName.isEmpty;
                    var emptyView = const Center(
                          child: TextView(
                            "请连接设备或选择设备进行调试",
                          ),
                        );
                    return PageView(
                      controller: tabController,
                      physics: const NeverScrollableScrollPhysics(),
                      children: <Widget>[
                        isEmpty? emptyView: HomePage(deviceId: viewModel.deviceId, packageName: viewModel.packageName),
                        isEmpty? emptyView: FileManagerPage(deviceId: viewModel.deviceId, packageName: viewModel.packageName),
                        isEmpty? emptyView: MediaManagementPage(deviceId: viewModel.deviceId, packageName: viewModel.packageName),
                        isEmpty? emptyView: LogcatPage(deviceId: viewModel.deviceId, packageName: viewModel.packageName),
                      ],
                    );
                  },
                ))
              ],
            ))
          ],
        ));
  }

  @override
  MainViewModel createViewModel() {
    return MainViewModel(context);
  }

  Widget menuView(String title, String menuTip, PopUpMenuButtonViewModel vm, Function refresh) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 16),
      child: Row(
        children: <Widget>[
          Text(title),
          const SizedBox(
            width: 10,
          ),
          PopUpMenuButton(
            viewModel: vm,
            menuTip: menuTip,
          ),
          // const SizedBox(
          //   width: 10,
          // ),
          // InkWell(
          //   onTap: () {
          //     refresh();
          //   },
          //   child: const Icon(
          //     Icons.refresh,
          //     color: Colors.black38,
          //   ),
          // ),
        ],
      ),
    );
  }

  _buildMainTabItem() {
    List<Widget> _children = [];
    for (var i = 0; i < viewModel.leftTabs.length; ++i) {
      var element = viewModel.leftTabs[i];
      _children.add(Consumer<MainViewModel>(builder: (context, value, child) {
        return InkWell(
          focusColor: AppColors.white,
          hoverColor: AppColors.e6,
          child: Container(
            color: i == viewModel.index ? AppColors.primary : Colors.transparent,
            alignment: Alignment.centerLeft,
            padding: const EdgeInsets.symmetric(vertical: 16, horizontal: 6),
            child: Text(
              element,
              style: const TextStyle(fontSize: 14, color: AppColors.e6),
            ),
          ),
          onTap: () {
            // tabController.index = i;
            tabController.jumpToPage(i);
            value.changeIndex(i);
          },
        );
      }));
    }
    return _children;
  }

  Widget packageNameView(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 16),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          const TextView("调试应用"),
          const SizedBox(
            width: 10,
            height: 30,
          ),
          InkWell(
            onTap: () {
              viewModel.packageSelect(context);
            },
            child: Container(
              alignment: Alignment.center,
              height: 44,
              child: Row(
                children: [
                  Selector<MainViewModel, String>(
                    selector: (context, viewModel) => viewModel.packageName,
                    builder: (context, packageName, child) {
                      return TextView(
                        packageName.isEmpty ? "未选择调试应用" : packageName,
                        color: const Color(0xFF666666),
                      );
                    },
                  ),
                  const SizedBox(
                    width: 5,
                  ),
                  const Icon(
                    Icons.arrow_drop_down,
                    color: Color(0xFF666666),
                  ),
                ],
              ),
            ),
          ),
          // const SizedBox(
          //   width: 10,
          // ),
          // InkWell(
          //   onTap: () {
          //     viewModel.getInstalledApp(viewModel.deviceId);
          //   },
          //   child: const Icon(
          //     Icons.refresh,
          //     color: Colors.black38,
          //   ),
          // ),
        ],
      ),
    );
  }
}
